//
//  MyPointsViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/19.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class MyPointsViewController: BaseViewController {

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initBaseLayout()
        layoutPageSubviews()
        configUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(tableView)
    }
    
    func layoutPageSubviews() {
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
    }
    
    func configUI() {
        self.view.backgroundColor = UIColor.white
        self.title = "我的积分"
        initNavItem(controller: self, imageName: "nav_back_n", highlitedImageName: "nav_back_h", selectedImageName: "", isLeft: true)
        initNavItem(controller: self, imageName: "nav_rule_n", highlitedImageName: "nav_rule_h", selectedImageName: "", isLeft: false)
    }
    
    // MAKR: - setters and getters
    var _tableView: UITableView!
    var tableView: UITableView {
        if _tableView == nil {
            _tableView = UITableView(frame: CGRect.zero, style: .grouped)
            _tableView.backgroundColor = UIColor(hexString: "#EEEEEE")
            _tableView.separatorStyle = .none
            _tableView.delegate = self
            _tableView.dataSource = self
            _tableView.register(MyPointsCell.self, forCellReuseIdentifier: "MyPointsCell")
            _tableView.register(MyPointsHeader.self, forHeaderFooterViewReuseIdentifier: "MyPointsHeader")
        }
        return _tableView
    }
    
}

extension MyPointsViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let string = "MyPointsCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: string, for: indexPath) as! MyPointsCell
        cell.configUI()
        
        if indexPath.row == 2 || indexPath.row == 4 {
            cell.sourceLabel.text = "赠送"
        }
        
        cell.index = indexPath.row
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 36
        }
        return 48
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let string = "MyPointsHeader"
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: string) as! MyPointsHeader
        header.configUI()
        header.withoutPoints = false
        
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 181
//        return 255
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
}
